<template>
  <el-dialog :title="action === 'add' ? '新增' : '修改'" width="40%" draggable 
              :close-on-click-modal="false" v-model="visible" @open="submitted = false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter="dataFormSubmit(dataFormRef)" label-width="180px">
      <el-form-item label="等级名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="等级名称" maxlength="50" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="等级需要的成长值" prop="growthPoint">
        <el-input-number v-model="dataForm.growthPoint" :min="1" :max="99999999" style="width: 100%"></el-input-number>
      </el-form-item>
      <el-form-item label="默认等级" prop="defaultStatus">
        <el-switch v-model="dataForm.defaultStatus" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="免运费标准" prop="freeFreightPoint">
        <el-input-number v-model="dataForm.freeFreightPoint" :min="1" :max="99999999" style="width: 100%"></el-input-number>
      </el-form-item>
      <el-form-item label="每次评价获取的成长值" prop="commentGrowthPoint">
        <el-input-number v-model="dataForm.commentGrowthPoint" :min="1" :max="99999999" style="width: 100%"></el-input-number>
      </el-form-item>
      <el-form-item label="免邮特权" prop="priviledgeFreeFreight">
        <el-switch v-model="dataForm.priviledgeFreeFreight" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="会员价格特权" prop="priviledgeMemberPrice">
        <el-switch v-model="dataForm.priviledgeMemberPrice" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
      <el-form-item label="生日特权" prop="priviledgeBirthday">
        <el-switch v-model="dataForm.priviledgeBirthday" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <el-form-item label="备注" prop="desct">
      <el-input type="textarea" v-model="dataForm.desct" maxlength="100" show-word-limit placeholder="备注"></el-input>
    </el-form-item>
    <template #footer>
      <div class="ad-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit(dataFormRef)" :disabled="submitted">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import http from "@/utils/httpRequest";
import validate from "@/utils/validate";
import type { FormInstance } from "element-plus";
import { ref, reactive, nextTick, defineExpose, defineEmits } from "vue";


// 操作：add-新增，update-修改
const action = ref("");

// 弹窗是否显示
const visible = ref(false);

// 提交状态
const submitted = ref(false);

// 表单数据
const dataForm = reactive({
  id: undefined,
  name: "",
  growthPoint: 10,
  defaultStatus: 0,
  freeFreightPoint: 20,
  commentGrowthPoint: 1,
  priviledgeFreeFreight: 0,
  priviledgeMemberPrice: 0,
  priviledgeBirthday: 0,
  desct: "",
});

// 数据校验规则
const dataRule = reactive({
  name: validate.required("等级名称"),
  growthPoint: validate.required("成长值"),
  defaultStatus: validate.required("默认等级"),
  freeFreightPoint: validate.required("免运费标准"),
  commentGrowthPoint: validate.required("评论成长值"),
  priviledgeFreeFreight: validate.required("免邮特权"),
  priviledgeMemberPrice: validate.required("会员特权价"),
  priviledgeBirthday: validate.required("生日特权"),
});

// 表单引用
const dataFormRef = ref<FormInstance>();

// 父级组件的自定义事件
const emit = defineEmits(['loadTableData']);

// 表单初始化
const initForm = async (a, id) => {
  action.value = a;
  // 新增操作
  if (a === "add") {
    visible.value = true;
    await nextTick();
    dataFormRef.value.resetFields();
    return;
  }
  // 修改操作
  dataForm.id = id;
  http
    .post({
      url: `/member/level/info?levelId=${dataForm.id}`,
    })
    .then(async ({ data }) => {
      if (http.success(data)) {
        visible.value = true;
        await nextTick();
        dataFormRef.value.resetFields();
        let form = data.body;
        Object.keys(form).forEach(function (key) {
          dataForm[key] = form[key];
        });
      }
    });
};

// 表单提交
const dataFormSubmit = (form: FormInstance | undefined) => {
  if (submitted.value) {
    return;
  }
  form.validate((valid) => {
    if (valid) {
      submitted.value = true;
      http
        .post({
          url: `/member/level/save`,
          data: dataForm,
        })
        .then(({ data }) => {
          if (http.success(data, true)) {
            visible.value = false;
            emit("loadTableData");
            return;
          }
          submitted.value = false;
        });
    }
  });
};

// 暴露方法
defineExpose({ initForm });
</script>
